package org.jeecg.modules.FinanceModule.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.FinanceModule.entity.MonthStatementEntity;
import org.jeecg.modules.FinanceModule.mapper.MonthStatementsMapper;
import org.jeecg.modules.FinanceModule.service.MonthStatementsService;
import org.jeecg.modules.zonglzp.entity.store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MonthStatementsServiceImpl implements MonthStatementsService {


    @Autowired
    MonthStatementsMapper mapper;
    @Override
    public IPage<MonthStatementEntity> getMonthStatementList(Page<MonthStatementEntity> page, int status, String realname) {
        return mapper.getMonthStatementList(page,status,realname);
    }

    @Override
    public int addMonthStatement(MonthStatementEntity entity) {

        //查询这个人目前的负债额度
        String olduserdebtLimitmapper=mapper.getdebtLimitbyuserId(Integer.parseInt(entity.getUserId()));

        BigDecimal userdebtLimitmapper=new BigDecimal(olduserdebtLimitmapper);
        //老的负债加上新的负债
        BigDecimal newdebtLimit=userdebtLimitmapper.add( entity.getBillMoney());
         //然后给客户update回去
        mapper.updateuserDebtLimit(Integer.parseInt(entity.getUserId()),newdebtLimit);

        entity.setBillId(String.valueOf(System.currentTimeMillis()));
        entity.setCreateTime(new Date());
        return mapper.addMonthStatement(entity);
    }

    /**
     *查询负债账单里的账单明细
     * @param page
     * @param billid
     * @param orderSn
     * @return
     */
    @Override
    public IPage<MonthStatementEntity> getsmallMonthStatementList(Page<MonthStatementEntity> page, String billid, String orderSn) {
        return mapper.getsmallMonthStatementList(page, billid,orderSn);
    }

    /**
     * 定时统计
     * @param userId
     */
    @Override
//    @Scheduled(cron = "0 15 12 * * 0-7")
    public void dingshiTongjizhangdan(String userId) {

        //先查询所有的用户id
       // List<Integer> userIdList=mapper.getuserIdList();
        List<Integer> userIdList=new ArrayList<Integer>();
        userIdList.add(Integer.parseInt(userId));
        for(int i=0;i<userIdList.size();i++){
          //首先，查出这个人 上个月 所有的月结账单
            List<MonthStatementEntity> UserSmallMonthList=mapper.getUserSmallMonthListbyuserId(userIdList.get(i));
            //查出这个人上个月所有月结账单金额总和
            String userlastMonthSum=mapper.getuserlastMonthSum(userIdList.get(i));
            if(userlastMonthSum==null){
                userlastMonthSum="0.0";
            }else {
                //查询这个人目前的负债额度
                String olduserdebtLimitmapper=mapper.getdebtLimitbyuserId(userIdList.get(i));
                BigDecimal userdebtLimitmapper=new BigDecimal(olduserdebtLimitmapper);
                //老的额度加上山个月欠的 加起来
                BigDecimal newdebtLimit=userdebtLimitmapper.add( new BigDecimal(userlastMonthSum));
                mapper.updateuserDebtLimit(userIdList.get(i),newdebtLimit);

                //新增一个月总账单账单
                long billId=System.currentTimeMillis();
                mapper.insertBillInfo(billId,new Date(),userlastMonthSum,userIdList.get(i));
                //将一条条月结账单，加入到总账单里
                if(UserSmallMonthList!=null){
                    for(int j=0;j<UserSmallMonthList.size();j++){
                        if(UserSmallMonthList.get(j).getIsJinzhang()!=1){
                            mapper.addsmallMonthlistToBill(billId,UserSmallMonthList.get(j).getId(),new Date());
                            //修改为已进账
                            mapper.updateMonthlistIsjinzhang(UserSmallMonthList.get(j).getId());
                        }
                    }
                }
            }
        }
    }



    public void dingshiTongjizhangdan2() {
        //先查询所有的用户id
        List<Integer> userIdList=mapper.getuserIdList();
        for(int i=0;i<userIdList.size();i++){
            //首先，查出这个人 上个月 所有的月结账单
            List<MonthStatementEntity> UserSmallMonthList=mapper.getUserSmallMonthListbyuserId(userIdList.get(i));
            //查出这个人上个月所有月结账单金额总和
            String userlastMonthSum=mapper.getuserlastMonthSum(userIdList.get(i));
            if(userlastMonthSum==null){
                userlastMonthSum="0.0";
            }else {
                //查询这个人目前的负债额度
                String olduserdebtLimitmapper=mapper.getdebtLimitbyuserId(userIdList.get(i));
                BigDecimal userdebtLimitmapper=new BigDecimal(olduserdebtLimitmapper);
                //老的额度加上山个月欠的 加起来
                BigDecimal newdebtLimit=userdebtLimitmapper.add( new BigDecimal(userlastMonthSum));
                mapper.updateuserDebtLimit(userIdList.get(i),newdebtLimit);
                //新增一个月总账单账单
                long billId=System.currentTimeMillis();
                mapper.insertBillInfo(billId,new Date(),userlastMonthSum,userIdList.get(i));
                //将一条条月结账单，加入到总账单里
                if(UserSmallMonthList!=null){
                    for(int j=0;j<UserSmallMonthList.size();j++){
                        mapper.addsmallMonthlistToBill(billId,UserSmallMonthList.get(j).getId(),new Date());
                    }
                }
            }

        }
    }
    /**
     *编辑账单
     * @param entity
     * @return
     */
    @Override
    public int updateMonthStatement(MonthStatementEntity entity) {
        return mapper.updateMonthStatement(entity);
    }

    //通过公司编码查询用户id
    @Override
    public String getuserIdbycompanyCode(String companyCode) {
        return mapper.getuserIdbycompanyCode( companyCode);
    }

    @Override
    public List<store> getstrorelist(String orgCode) {
        return mapper.getstrorelist( orgCode);
    }


}
